package com.ld.businessCard.filter;

import com.ld.businessCard.entity.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UserFilter implements Filter {
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		HttpServletResponse response = (HttpServletResponse) servletResponse;

		if(user != null){
			filterChain.doFilter(request,response);
		}else {
			response.sendRedirect("/index.jsp");
		}


	}

	public void destroy() {

	}
}
